package com.aierjun.contact;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.aierjun.testservice.R;

/**
 * Created by Administrator on 2016/7/1.
 */
public class TestContactActivity extends AppCompatActivity{
    private static final int REQUEST_CODE_CHOOSE_CONTACT=100;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_contact);
    }
    public void chooseContact(View view){
        Intent intent =new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent,REQUEST_CODE_CHOOSE_CONTACT);
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (requestCode!=REQUEST_CODE_CHOOSE_CONTACT||requestCode!=RESULT_OK) return;
        Uri uri=data.getData();
        Cursor cursor=getContentResolver().query(uri,null,null,null,null);
        if (cursor==null) return;
        cursor.moveToFirst();
        String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Cursor phoneCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,null,null);
        while (phoneCursor!=null&&phoneCursor.moveToNext()){
            String phoneNum=phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Log.d("AierjunLog",phoneNum);
        }
    }
}
